// import com.alibaba.druid.FastsqlException;
// import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
// import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.io.IOException;

#include "MySqlCharExpr.h"
#include "../../visitor/MySqlASTVisitor.h"
#include "../../../../../Exception/FastsqlException.h"
#include "../../../../visitor/SQLASTVisitor.h"
#include "../../../../../utils/instanceof.h"

MySqlCharExpr::MySqlCharExpr()
{
}

MySqlCharExpr::MySqlCharExpr(string_ptr text)
    : SQLCharExpr(text)
{
}

MySqlCharExpr::MySqlCharExpr(string_ptr text, string_ptr charset)
    : SQLCharExpr(text)
{
  this->charset = charset;
}

MySqlCharExpr::MySqlCharExpr(string_ptr text, string_ptr charset, string_ptr collate)
    : SQLCharExpr(text)
{
  this->charset = charset;
  this->collate = collate;
}

string_ptr MySqlCharExpr::getCharset()
{
  return charset;
}

void MySqlCharExpr::setCharset(string_ptr charset)
{
  this->charset = charset;
}

string_ptr MySqlCharExpr::getCollate()
{
  return collate;
}

void MySqlCharExpr::setCollate(string_ptr collate)
{
  this->collate = collate;
}

string_ptr MySqlCharExpr::getType()
{
  return type;
}

void MySqlCharExpr::setType(string_ptr type)
{
  this->type = type;
}

void MySqlCharExpr::output(std::stringstream * buf)
{
  try
  {
    if (charset != nullptr)
    {
      (*buf)<<charset->c_str();
      (*buf)<<" ";
    }
    if (SQLCharExpr::text != nullptr)
    {
      SQLCharExpr::output(buf);
    }

    if (collate != nullptr)
    {
     (*buf)<<" COLLATE ";
      (*buf)<<collate->c_str();
    }
  }
  catch (const std::exception &e)
  {
    std::string tmp = "output error";
    tmp += e.what();
    throw new FastsqlException(make_string_ptr(tmp));
  }
}

// @Override
void MySqlCharExpr::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, MySqlASTVisitor>(visitor))
  {
    accept0(std::dynamic_pointer_cast<MySqlASTVisitor>(visitor));
  }
  else
  {
    visitor->visit(SharedObject(MySqlCharExpr));
    visitor->endVisit(SharedObject(MySqlCharExpr));
  }
}

void MySqlCharExpr::accept0(MySqlASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(MySqlCharExpr));
  visitor->endVisit(SharedObject(MySqlCharExpr));
}

string_ptr MySqlCharExpr::toString()
{
  std::stringstream  buf ;
  output(&buf);
  return make_string_ptr(buf.str());
}

SQLObject_ptr MySqlCharExpr::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<MySqlCharExpr>(new MySqlCharExpr(text));
  std::shared_ptr<MySqlCharExpr> x = std::dynamic_pointer_cast<MySqlCharExpr>(clone_tmp);
  // MySqlCharExpr x = new MySqlCharExpr(text);
  x->collate = collate;
  x->charset = charset;
  x->type = type;

  return x;
}
